/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.upf.grupE.interf;

import edu.upf.grupE.impl.GUILogic;
import edu.upf.grupE.interftest.EventListenerImpl;
import edu.upf.grupE.interftest.GUIEventListener;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;


import javax.swing.*;


import org.jdesktop.swingx.JXCollapsiblePane;
import org.jdesktop.swingx.JXPanel;
import org.jdesktop.swingx.JXTitledPanel;

/**
 *
 * @author Oscar
 */
public class MainPage {

    private static JPanel actual;
    private static JXCollapsiblePane p;
    private static JXPanel central;
    private static JButton Search;
    private static JButton jButton1;
    private static JButton jButton2;
    private static JButton jButton3;
    private static JButton buddies;
    private static JLabel state;
    private static Search search;
    private static ImageSelection images;
    private static VideoSelection videos;

    /**
     *
     * @param args
     */
    public static void main(String[] args) {
        MainPage page = new MainPage();

    }

    public MainPage() {


        initComponents();
    }

    public void initComponents() {

        //Init the frame
        final MainFrame frame = new MainFrame();
        frame.setLayout(new BorderLayout());

        //North Panel, with all the above buttons
        JXPanel north = new JXPanel(new BorderLayout());
        JXPanel northdown = new JXPanel(new BorderLayout());

        JXCollapsiblePane highest = new JXCollapsiblePane(JXCollapsiblePane.Direction.UP, new BorderLayout());
        highest.setCollapsed(true);
        JXPanel northhigh = new JXPanel(new BorderLayout());

        JXPanel northmiddle = new JXPanel();
        JXTitledPanel downl = new JXTitledPanel("Downloads");
        JPanel downloads = new Downloads();
        downl.setContentContainer(downloads);
        highest.add("Center", downl);

        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        Search = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();

        north.setPreferredSize(new java.awt.Dimension(952, 70));
        //setVerifyInputWhenFocusTarget(false);

        jButton1.setIcon(new ImageIcon(MainPage.class.getResource("/img/icon_camera.gif"))); // NOI18N
        jButton1.setText("Images");
        jButton1.setBorderPainted(false);
        jButton1.setContentAreaFilled(false);
        jButton1.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {

                changePanel(1);
            }
        });

        jButton2.setIcon(new ImageIcon(MainPage.class.getResource("/img/icono_video.gif"))); // NOI18N
        jButton2.setText("Videos");
        jButton2.setBorderPainted(false);
        jButton2.setContentAreaFilled(false);
        jButton2.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {

                changePanel(2);
            }
        });


        Search.setIcon(new ImageIcon(MainPage.class.getResource("/img/system-search.jpg"))); // NOI18N
        Search.setText("Search");
        Search.setBorderPainted(false);
        Search.setContentAreaFilled(false);
        Search.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {

                changePanel(3);
            }
        });


        jButton3.setBorderPainted(false);
        jButton3.setContentAreaFilled(false);

        jButton3.setAction(highest.getActionMap().get(JXCollapsiblePane.TOGGLE_ACTION));

        jButton3.setIcon(new javax.swing.ImageIcon(MainPage.class.getResource("/img/pybackpack.png"))); // NOI18N

        jButton3.setText("Downloads");
        jButton3.setIcon(new javax.swing.ImageIcon(MainPage.class.getResource("/img/pybackpack.png"))); // NOI18N
        northmiddle.add(jButton1);
        northmiddle.add(jButton2);
        northmiddle.add(Search);
        northmiddle.add(jButton3);

        northdown.add("West", northmiddle);


        north.add(highest, BorderLayout.NORTH);




        frame.add(highest, BorderLayout.PAGE_START);

        //Definition of the iamge edition button
        p = new JXCollapsiblePane();
        p.setCollapsed(true);
        p.setLayout(new BorderLayout());
        p.add("Center", new EditionButtons());

        // Definition of the central panel and all the menus that are going to be displayed
        central = new JXPanel(new BorderLayout());
        MainPanel main = new MainPanel();
        actual = main;
        main.setVisible(true);
        search = new Search();
        images = new ImageSelection(p);

        videos = new VideoSelection();
        search.setVisible(false);
        images.setVisible(false);
        central.add("North", northdown);
        central.add("West", main);

        frame.add("Center", central);

        //Inferior Panel. It contains a login button, a statte label and a Chat
        final JButton login = new JButton("Login");

        login.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                if (login.getText() == "Login") {
                    Login log = new Login();
                    log.setVisible(true);
                    log.addWindowListener(new WindowAdapter() {

                        public void windowClosed(WindowEvent e) {
                            login.setText("Logout");
                            buddies.setEnabled(true);
                            state.setIcon(new javax.swing.ImageIcon(MainPage.class.getResource("/img/user_online.gif"))); // NOI18N


                        }
                    });
                } else {
                    login.setText("Login");
                    buddies.setEnabled(false);
                    state.setIcon(new javax.swing.ImageIcon(MainPage.class.getResource("/img/user_offline.gif"))); // NOI18N
                }
            }
        });

        //Definition of the chat collpasiblePane
        JXCollapsiblePane cp2 = new JXCollapsiblePane(new BorderLayout());

        cp2.setCollapsed(true);
        JXTitledPanel b = new JXTitledPanel("Buddies");
        JPanel online = new Buddies();

        b.setContentContainer(online);


        cp2.add("Center", b);



        JXPanel down = new JXPanel(new BorderLayout());
        JXPanel downright = new JXPanel(new BorderLayout());

        JXPanel downhigh = new JXPanel();

        JXPanel downdown = new JXPanel(new BorderLayout());

        JXPanel downleft = new JXPanel(new BorderLayout());
        downhigh.add("South", cp2);


        login.setContentAreaFilled(false);
        login.setBorderPainted(false);
        downleft.add("West", login);
        JPanel auxiliar = new JPanel(new BorderLayout());

        buddies = new JButton();
        buddies.setContentAreaFilled(false);
        buddies.setBorderPainted(false);
        buddies.setAction(cp2.getActionMap().get(JXCollapsiblePane.TOGGLE_ACTION));
        buddies.setText("Chat");
        buddies.setEnabled(false);
        auxiliar.add("Center", buddies);

        central.add("South", p);

        state = new JLabel();
        state.setIcon(new javax.swing.ImageIcon(MainPage.class.getResource("/img/user_offline.gif"))); // NOI18N
        auxiliar.add("East", state);

        downdown.add("Center", auxiliar);
        downright.add("South", downdown);

        down.add("North", cp2);
        down.add("West", downleft);
        down.add("East", downright);

        frame.add("South", down);


        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        frame.setVisible(true);
    }

    /**
     *
     * @param type, which defines the changes of the panels
     */
    public static void changePanel(int type) {
        if (type == 1 && actual != images) {
            p.setCollapsed(true);
            central.add("West", images);
            images.setVisible(true);
            actual.setVisible(false);
            actual = images;



        }
        if (type == 2 && actual != videos) {
            p.setCollapsed(true);
            central.add("West", videos);
            videos.setVisible(true);
            actual.setVisible(false);
            actual = videos;
        }
        if (type == 3 && actual != search) {
            p.setCollapsed(true);
            central.add("West", search);
            search.setVisible(true);
            actual.setVisible(false);
            actual = search;

        }
    }
}
